home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS / h / AToken.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-14  |  3.9 KB  |  115 lines  |  [TEXT/MPS ]

  1. /* ANTLRToken.h
  2.  *
  3.  * SOFTWARE RIGHTS
  4.  *
  5.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  6.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  7.  * company may do whatever they wish with source code distributed with
  8.  * PCCTS or the code generated by PCCTS, including the incorporation of
  9.  * PCCTS, or its output, into commerical software.
  10.  * 
  11.  * We encourage users to develop software with PCCTS.  However, we do ask
  12.  * that credit is given to us for developing PCCTS.  By "credit",
  13.  * we mean that if you incorporate our source code into one of your
  14.  * programs (commercial product, research project, or otherwise) that you
  15.  * acknowledge this fact somewhere in the documentation, research report,
  16.  * etc...  If you like PCCTS and have developed a nice tool with the
  17.  * output, please mention that you developed it using PCCTS.  In
  18.  * addition, we ask that this header remain intact in our source code.
  19.  * As long as these guidelines are kept, we expect to continue enhancing
  20.  * this system and expect to make other tools available as they are
  21.  * completed.
  22.  *
  23.  * ANTLR 1.23
  24.  * Terence Parr
  25.  * Parr Research Corporation
  26.  * with Purdue University and AHPCRC, University of Minnesota
  27.  * 1989-1994
  28.  */
  29.  
  30. #ifndef ATOKEN_H_GATE
  31. #define ATOKEN_H_GATE
  32.  
  33. #include <string.h>
  34.  
  35. #ifndef ANTLRCommonTokenTEXTSIZE
  36. #define ANTLRCommonTokenTEXTSIZE        100
  37. #endif
  38.  
  39. /* must define what a char looks like; can make this a class too */
  40. typedef char ANTLRChar;
  41.  
  42. class ANTLRAbstractToken {
  43. };
  44.  
  45. // If the user subclasses this one, then they must do their own syn()
  46. // routine and FAIL() routine.
  47. class ANTLRLightweightToken : public ANTLRAbstractToken {
  48. protected:
  49.         TokenType _type;
  50. public:
  51.         TokenType getType() { return _type; }
  52.         void setType(TokenType t) { _type = t; }
  53. };
  54.  
  55. // What does an ANTLR Token look like?
  56. // This has virtual functions so that minimum token
  57. // size >= sizeof('int') + sizeof(vtbl pointer).
  58. // If you want a totally light weight token and can make your own scanner
  59. // define ANTLRToken by itself without subclassing ANTLRTokenBase; or, you
  60. // can subclass the lightweight token above.
  61. class ANTLRTokenBase : public ANTLRLightweightToken {
  62. private:
  63.     static ANTLRTokenBase t;    /* this is here, not in makeToken(), because
  64.                                  * some compilers can't handle inlines with
  65.                                  * static local variables.
  66.                                  */
  67. public:
  68.     /* Define to satisfy ANTLR error mechanism */
  69.     virtual int line() { return 0; }
  70.     virtual void setLine(int line) { ; }
  71.     virtual ANTLRChar *getText() { return (ANTLRChar *)""; }
  72.     virtual void setText(ANTLRChar *) {;}
  73.     virtual ANTLRLightweightToken *makeToken(TokenType tt, ANTLRChar *txt, int line)
  74.             {
  75.                 t.setType(tt); t.setText(txt); t.setLine(line);                    return &t;
  76.             }
  77. };
  78.  
  79. // if you are using DLG, your token def must derive from this class
  80. // min Token size is sizeof(TokenType) + space for virtual function table.
  81. class DLGBasedToken : public ANTLRTokenBase {
  82.     int _line;
  83. public:
  84.     int line() { return _line; }
  85.     void setLine(int line) { _line = line; }
  86.     DLGBasedToken(TokenType t) { setType(t); }
  87.     DLGBasedToken() {;}
  88.     virtual void setText(ANTLRChar *s) = 0;
  89.     virtual ANTLRChar *getText() = 0;
  90.     virtual ANTLRLightweightToken *makeToken(TokenType tt, ANTLRChar *txt, int line) = 0;
  91. };
  92.  
  93. class ANTLRCommonToken : public DLGBasedToken {
  94. private:
  95.     static ANTLRCommonToken t;
  96. protected:
  97.     ANTLRChar _text[ANTLRCommonTokenTEXTSIZE+1];
  98. public:
  99.     ANTLRCommonToken(TokenType t, ANTLRChar *s) : DLGBasedToken(t)
  100.             { setText(s); }
  101.     ANTLRCommonToken() {;}
  102.     ANTLRChar *getText() { return _text; }
  103.     void setText(ANTLRChar *s) { strncpy((char *)_text, (char *)s, ANTLRCommonTokenTEXTSIZE); }
  104.     virtual ANTLRLightweightToken *makeToken(TokenType tt, ANTLRChar *txt, int line)
  105.         {
  106.             ANTLRCommonToken *t = new ANTLRCommonToken;
  107.             t->setType(tt); t->setText(txt); t->setLine(line);
  108.             return t;
  109.         }
  110. };
  111.  
  112. typedef ANTLRCommonToken ANTLRCommonBacktrackingToken;
  113.  
  114. #endif
  115.